HAL PWR initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 PWR configuration panel.

  • Configure the SW layer HAL.

  • Configure the PWR main features.

  • Configure the additional PWR features if needed.

  • Configure the PWR GPIO pins.

  • Configure the PWR DMA(s) if the application requires to use the PWR in DMA mode.

  • Configure the NVIC if the application requires the PWR to be used in IT mode.

  • Generate the code.

HAL PWR configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL PWR additional features configuration

Configure the additional PWR features if needed.

PWR additional feature configuration

GPIO configuration

Configure GPIO pins.

PWR GPIO configuration

EXTI configuration

Configure EXTI lines.

PWR EXTI configuration

Generated PWR initialization and de-initialization sequences

The PWR initialization sequence is generated in the mx_pwr_init and mx_pwr_deinit functions in mx_pwr.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated: PWR initialization and de-initialization sequences

Topic

Code Snippet HAL1

Code Snippet HAL2

Init, config and Msp sequence function

static void SystemPower_Config(void)
{
  PWR_PVDTypeDef sConfigPVD = {0};

 /*
  * PVD Configuration
  */
  sConfigPVD.PVDLevel = PWR_PVDLEVEL_7;
  sConfigPVD.Mode = PWR_PVD_MODE_NORMAL;
  HAL_PWR_ConfigPVD(&sConfigPVD);

 /*
  * Enable the PVD Output
  */
  HAL_PWR_EnablePVD();
  /* PVD_PVM_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(PVD_PVM_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(PVD_PVM_IRQn);
}
system_status_t mx_pwr_init(void)
{
  /****************************************************************************/
  /* Configuration of PVD (Programmable Voltage Detector)                     */
  /****************************************************************************/
  /* Enable PVD */
  HAL_PWR_EnableProgrammableVoltageDetector();

  /* Configure PVD EXTI Line */
  hal_exti_config_t exti_config;

  /* Initialize the EXTI for line 16 */
  HAL_EXTI_Init(&hEXTI16, HAL_EXTI_LINE_16);

  /* Set the trigger as RISING */
  exti_config.trigger = HAL_EXTI_TRIGGER_RISING;
  HAL_EXTI_SetConfig(&hEXTI16, &exti_config);

  /* Set line 16 Interrupt priority */
  HAL_CORTEX_NVIC_SetPriority(PWR_PVD_IRQn,
                              HAL_CORTEX_NVIC_PREEMP_PRIORITY_0,
                              HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(PWR_PVD_IRQn);
  return SYSTEM_OK;
}

Deinit sequence function

static void SystemPower_Config(void)
{
  /*
  * Disable the PVD Output
  */
  HAL_PWR_DisablePVD();

  /* PVD_PVM_IRQn interrupt configuration */
  HAL_NVIC_DisableIRQ(PVD_PVM_IRQn);
}
system_status_t mx_pwr_deinit(void)
{
  /* Deinit PVD EXTI Line */
  /* De-initialize the EXTI for line16 */
  HAL_EXTI_DeInit(&hEXTI16);

  /* Set line 16 Interrupt priority */
  HAL_CORTEX_NVIC_DisableIRQ(PWR_PVD_IRQn);
  return SYSTEM_OK;
}

PWR IRQ handler sequence

void PVD_PVM_IRQHandler(void)
{
  HAL_PWR_PVD_IRQHandler();
}
 void PWR_PVD_IRQHandler(void)
{
  HAL_PWR_IRQHandler(&hEXTI);
}